import { login } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'

const user = {
  namespaced: true,
  state: {
    token: getToken(), // vue 的token需要从本地去，防止刷新后没有token
    userInfo: {}
  },
  mutations: {
    setToken(state, token) {
      state.token = token
      setToken(token)
    },
    removeToken(state) {
      state.token = null
      removeToken()
    },
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    async handleLogin(ctx, payload) {
      console.log(1)
      console.log(payload)
      const { token } = await login(payload)
      console.log(token)
      ctx.commit(`setToken`, token)
    }

  }
}

export default user

